package com.may.wlrj.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.may.wlrj.entity.SysEntity;
import com.may.wlrj.entity.UserEntity;
import com.may.wlrj.entity.UserLogEntity;
import com.may.wlrj.services.UserService;
import com.may.wlrj.util.CryptoUtil;
import com.may.wlrj.util.SysUtil;
import com.may.wlrj.util.XinLongUtil;
@Controller
public class RouteController {
	private UserService userService;
    @Autowired
    public void setUserService(UserService userService){
        this.userService = userService;
    } 
    
    
    /**
     * 向用户登录页面跳转
     */
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String toLogin(){    	
        //重定向到登录页面的跳转方法
        return "login.html";
    }
	/**
     * 用户登录
     * @param user
     * @param model
     * @param session
     * @return
     * @throws Exception 
     * @throws IOException 
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST,produces="text/html;charset=UTF-8;")
    public @ResponseBody String login(@RequestBody String objnode,HttpServletRequest request, HttpSession session) throws IOException, Exception{
        //获取用户名和密码
    	System.out.println("---------------------->>>>login");
    	String reslut = userService.userAuth(objnode,request,session);
    	return reslut;
        
        
    }
    
    @RequestMapping(value = "/index")
    public String toMain(){
    	System.out.println("---------------------->>>>toMain");
        return "index";
    }
    
    @RequestMapping(value = "/usermanager")
    public String userManager(HttpServletRequest request ,HttpServletResponse response,HttpSession session){

        return "usermanager.html";
    }

    
    @RequestMapping(value = "/")
    public String toIndex(){
    	System.out.println("---------------------->>>>toIndex");
        return "index";
    }
    
    @RequestMapping(value = "/plugins")
    public String plugins(HttpServletRequest request ,HttpServletResponse response,HttpSession session){

        return "plugins.html";
    }    
     
    @RequestMapping(value = "/organization")
    public String organization(HttpServletRequest request ,HttpServletResponse response,HttpSession session){
        return "organization.html";
    }
    
    @RequestMapping(value = "/rolemanager")
    public String rolemanager(HttpServletRequest request ,HttpServletResponse response,HttpSession session){
        return "rolemanager.html";
    }
    
    @RequestMapping(value = "/rightmanager")
    public String rightmanager(HttpServletRequest request ,HttpServletResponse response,HttpSession session){
        return "rightmanager.html";
    }
    
    @RequestMapping(value = "/finger")
    public String finger(HttpServletRequest request ,HttpServletResponse response,HttpSession session){
        return "finger.html";
    }
    
    @RequestMapping(value = "/usermanagerlog")
    public String usermanagerlog(HttpServletRequest request ,HttpServletResponse response,HttpSession session){
        return "usermanagerlog.html";
    }
    
    @RequestMapping(value = "/usersyslog")
    public String usersyslog(HttpServletRequest request ,HttpServletResponse response,HttpSession session){
        return "usersyslog.html";
    }
    
    @RequestMapping(value = "/user_right_auth")
    public String user_right_auth(HttpServletRequest request ,HttpServletResponse response,HttpSession session){
        return "user_right_auth.html";
    }
    
    @RequestMapping(value = "/sysconfig")
    public String sysconfig(HttpServletRequest request ,HttpServletResponse response,HttpSession session){
        return "sysconfig.html";
    }
    
    @RequestMapping(value = "/lockmanage")
    public String capitalcheckmission(HttpServletRequest request ,HttpServletResponse response,HttpSession session){
        return "lockmanage.html";
    }
    
    @RequestMapping(value = "/lockmission")
    public String lockmission(HttpServletRequest request ,HttpServletResponse response,HttpSession session){
        return "lockmission.html";
    }    
    
    @RequestMapping(value = "/getorgtree")
    public String orgtreeget(HttpServletRequest request ,HttpServletResponse response){
        return "orgtree.html";
    }
    
    @RequestMapping(value = "/getlockstable")
    public String getlockstable(HttpServletRequest request ,HttpServletResponse response){
        return "lockselect.html";
    }
    
    @RequestMapping(value = "/template_locklist")
    public String template_locklist(HttpServletRequest request ,HttpServletResponse response){
        return "template_locklist.html";
    }
    
    @RequestMapping(value = "/template_locklist_single")
    public String template_locklist_single(HttpServletRequest request ,HttpServletResponse response){
        return "template_locklist_single.html";
    }
    
    @RequestMapping(value = "/template_multilocklist")
    public String template_multilocklist(HttpServletRequest request ,HttpServletResponse response){
        return "template_multilocklist.html";
    }
    
    @RequestMapping(value = "/template_userlistByLock")
    public String template_userlistByLock(HttpServletRequest request ,HttpServletResponse response){
        return "template_userlistByLock.html";
    }
    
    @RequestMapping(value = "/template_userlist_single")
    public String template_userlist_single(HttpServletRequest request ,HttpServletResponse response){
        return "template_userlist_single.html";
    }
    
    @RequestMapping(value = "/template_rolelist_single")
    public String template_rolelist_single(HttpServletRequest request ,HttpServletResponse response){
        return "template_rolelist_single.html";
    }
    
    @RequestMapping(value = "/template_mission_detail")
    public String template_mission_detail(HttpServletRequest request ,HttpServletResponse response){
        return "template_mission_detail.html";
    }
    
    @RequestMapping(value = "/template_finger")
    public String template_finger(HttpServletRequest request ,HttpServletResponse response){
        return "template_finger_zz.html";
    }
    
    @RequestMapping(value = "/userlist")
    public String userlist(HttpServletRequest request ,HttpServletResponse response){
        return "userlist.html";
    }
    
    @RequestMapping(value = "/lockofflog")
    public String lockofflog(HttpServletRequest request ,HttpServletResponse response){
        return "lockofflog.html";
    }
    
    @RequestMapping(value = "/allotmission")
    public String multilockmission(HttpServletRequest request ,HttpServletResponse response){
        return "allotmission.html";
    }
    
    @RequestMapping(value = "/locktransfer")
    public String locktransfer(HttpServletRequest request ,HttpServletResponse response){
        return "locktransfer.html";
    }
    
    @RequestMapping(value = "/keymanage")
    public String keymanage(HttpServletRequest request ,HttpServletResponse response){
        return "keymanage.html";
    }
    
    @RequestMapping(value = "/openlog")
    public String openlog(HttpServletRequest request ,HttpServletResponse response){
        return "openlog.html";
    }
    
    @RequestMapping(value = "/lockreg")
    public String lockreg(HttpServletRequest request ,HttpServletResponse response){
        return "lockreg.html";
    }
    
    @RequestMapping(value = "/logout",method=RequestMethod.GET)
    public String logout(HttpServletRequest request ,HttpServletResponse response,HttpSession session){
    	String remoteaddr = XinLongUtil.getRemoteAddr(request);
    	UserEntity sentity = (UserEntity)session.getAttribute("USER_SESSION");
    	Date date = new Date();
		SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		UserLogEntity ulog = new UserLogEntity();
		ulog.setId(UUID.randomUUID().toString().replace("-", ""));
		ulog.setRemote_addr(remoteaddr);
		ulog.setOrgid(sentity.getOrg_id());
		ulog.setCreatetime(dateFormat.format(date));
		ulog.setRname("注销");
		ulog.setCreateusername(sentity.getUsername());
		XinLongUtil.saveUserLog(ulog);
        //清除session
        session.invalidate();
        //重定向到登录页面的跳转方法
        return "login.html";
    }
}
